﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; 
using System.Data.Sql;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
namespace HZ.Supervise.Web.Secondphase.ReportArea
{
    public partial class ReportOfDurgFirstLevel : BasePage
    {
        DataTable dt = new DataTable();
        HZ.Supervise.IBLL.IBLL_PreWarning pre = HZ.Supervise.BLLFactory.IBLL_PreWarning();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                Databind();
                BindChart();
                GetStatistics();
            }
        }

        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//采购总金额
            double PurchaseCountTotal = 0;
            double wareHouseAmountTotal = 0;//入库总金额
            double wareHouseCountTotal = 0;
            for (int i = 0; i < gvwsort.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwsort.Rows[i].Cells[2].Text);
                PurchaseCountTotal += double.Parse(gvwsort.Rows[i].Cells[3].Text);
                wareHouseAmountTotal += double.Parse(gvwsort.Rows[i].Cells[4].Text);
                wareHouseCountTotal += double.Parse(gvwsort.Rows[i].Cells[5].Text);
            }
            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";采购总数量为:" + PurchaseCountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString() + ";入库总数量为:" + wareHouseCountTotal.ToString();

        }
        public void Databind()
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            //string timeFrom = DateTime.Now.AddMonths(-12).Year + "-" + DateTime.Now.AddMonths(-12).Month;
            //sq.Append += "PurchaseTimeMonth > '" + timeFrom + "'";
            int t = 0;
            //HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            //ie.QueryObject = new HZ.Supervise.Model.QueryObject()
            //{
            //    //分组字段
            //    GroupField = GroupByField.PurchaseTimeMonth | GroupByField.SortSource,
            //    //查询条件
            //    Query = sq,
            //    //分组查询字段
            //    GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.WareHouseRate | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.PurchaseCountTotal | GroupFields.DistributeRate,
            //    //排序字段
            //    SortField = "PurchaseTimeMonth,SortSource ASC"
            //};

            //将数据填入gridview中

            //System.Data.DataTable dt2 =ie.FixAllMonth(ie.ExecutePage(1, 0, out t),2,0,"PurchaseTimeMonth");
           
            //显示相应的字段
            HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();
            var model = new HZ.Data.QueryModel();
            model["SortFields"] = "DayTime,SortId ";
            model["SortName"] = sq.SortName;
            string month = "";
            if (DateTime.Now.AddMonths(-12).Month <= 9) month = "0" + DateTime.Now.AddMonths(-12).Month.ToString();
            else month = DateTime.Now.AddMonths(-12).Month.ToString();
            string timeFrom = DateTime.Now.AddMonths(-12).Year + "" + month;
            model["append"] = "DayTime > '" + timeFrom + "'";

            //DateTime timeTo = DateTime.Parse(DateTime.Now.Year + "/" + DateTime.Now.Month);
            //model["DayTimeFrom"] = timeFrom;
            //model["DayTimeTo"] = timeTo;
            System.Data.DataTable dtt = iNew.DayTime_SortSore_SortName_GroupBy(model, 1, 0, out t);

            //gvwsort.DataSource = dt;
            //gvwsort.DataBind();
          
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            dtt = ie.FixAllMonth(dtt, 2, 0, "DayTime");
            gvwsort.DataSource = dtt;
            gvwsort.DataBind();
            dt = dtt.Copy();

            for (int j = 0; j < gvwsort.Rows.Count; j++)
            {
                //名称相同才能合并
                //防止超出索引
                if (j < (gvwsort.Rows.Count - 1))
                {
                    if (gvwsort.Rows[j].Cells[0].Text.Trim() == gvwsort.Rows[j + 1].Cells[0].Text.Trim())
                    {
                        //合并头
                        gvwsort.Rows[j].Cells[0].RowSpan = 2;
                        gvwsort.Rows[j + 1].Cells[0].Visible = false;

                        //合并尾
                        gvwsort.Rows[j].Cells[7].RowSpan = 2;
                        gvwsort.Rows[j + 1].Cells[7].Visible = false;
                        j++;
                    }
                    else
                    {

                    }
                }

                //合并

            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {

        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {

        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {

        }
        protected void lbtnSee_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 图表绑定 
        /// </summary>
        private void BindChart()
        {

            //空为初始页
            if (dt.Rows.Count > 0)
            {
                string monch = "";//月份
                int count = 2;//次数
                string sort = "0";//属性 0:基药 1:非基药

                #region 左图

                //***************** 基本药品金额*****************/
                Series series1 = new Series("基本药品采购金额");
                series1.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series1.BorderWidth = 1;
                series1.ShadowOffset = 1;
                series1["DrawingStyle"] = "Emboss";
                //***************** 非基本药品金额*******************/
                Series series2 = new Series("非基本药品采购金额");
                series2.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series2.BorderWidth = 1;
                series2.ShadowOffset = 1;
                series2["DrawingStyle"] = "Emboss";
                //***********************基本药品数量******************/
                Series series3 = new Series("基本药品采购数量");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;
                series3["DrawingStyle"] = "Emboss";
                //***********************非基本药品数量******************/
                Series series4 = new Series("非基本药品采购数量");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                series4["DrawingStyle"] = "Emboss";
                #endregion

                #region 右图

                //***************** 基本药品金额 柱状  ****************/
                Series series5 = new Series("基本药品入库金额");
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series5.BorderWidth = 1;
                series5.ShadowOffset = 1;
                series5["DrawingStyle"] = "Emboss";

                //***************** 非基本药品金额 柱状  **************/
                Series series6 = new Series("非基本药品入库金额");
                series6.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series6.BorderWidth = 1;
                series6.ShadowOffset = 1;
                series6["DrawingStyle"] = "Emboss";
                //***************** 基本药品数量 曲线******************/
                Series series7 = new Series("基本药品入库数量");
                series7.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series7.BorderWidth = 1;
                series7.ShadowOffset = 1;
                series7["DrawingStyle"] = "Emboss";
                //***************** 非基本药品数量 曲线****************/
                Series series8 = new Series("非基本药品入库数量");
                series8.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "StackedColumn", true);
                series8.BorderWidth = 1;
                series8.ShadowOffset = 1;
                series8["DrawingStyle"] = "Emboss";
                #endregion



                #region 绑定值
                int s0 = 0, s1 = 0;
                if (dt.Rows[0]["SortSorce"].ToString().Equals("1"))
                {
                    count = 1;
                    monch = dt.Rows[1]["DayTime"].ToString();
                }
                for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                {
                    if (dt.Rows[i]["DayTime"].ToString().Equals(monch))
                    {
                        if (i == 0)
                        {
                            series1.Points.AddXY(monch.Substring(4) + "月", 0);
                            series3.Points.AddXY(monch.Substring(4) + "月", 0);
                            series5.Points.AddXY(monch.Substring(4) + "月", 0);
                            series7.Points.AddXY(monch.Substring(4) + "月", 0);
                            series1.Points[s0].ToolTip = "0";
                            series3.Points[s0].ToolTip = "0";
                            series5.Points[s0].ToolTip = "0";
                            series7.Points[s0].ToolTip = "0";
                            s0++;
                        }
                        series2.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                        series4.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                        series6.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                        series8.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                        series2.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                        series4.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                        series6.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                        series8.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                        s1++;
                        count++;
                        sort = "1";

                    }
                    else
                    {
                        if (count == 2 && dt.Rows[i]["SortSorce"].ToString().Equals("0"))
                        {
                            series1.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series3.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                            series5.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                            series7.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                            series1.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                            series3.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                            series5.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                            series7.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                            s0++;
                            count = 1;
                            sort = "0";
                            monch = dt.Rows[i]["DayTime"].ToString();
                        }
                        else
                        {
                            if (sort.Equals("0"))
                            {
                                series2.Points.AddXY(monch.Substring(4) + "月", 0);
                                series4.Points.AddXY(monch.Substring(4) + "月", 0);
                                series6.Points.AddXY(monch.Substring(4) + "月", 0);
                                series8.Points.AddXY(monch.Substring(4) + "月", 0);
                                series2.Points[s1].ToolTip = "0";
                                series4.Points[s1].ToolTip = "0";
                                series6.Points[s1].ToolTip = "0";
                                series8.Points[s1].ToolTip = "0";
                                s1++;

                            }
                            else
                            {
                                series1.Points.AddXY(monch.Substring(4) + "月", 0);
                                series3.Points.AddXY(monch.Substring(4) + "月", 0);
                                series5.Points.AddXY(monch.Substring(4) + "月", 0);
                                series7.Points.AddXY(monch.Substring(4) + "月", 0);
                                series1.Points[s0].ToolTip = "0";
                                series3.Points[s0].ToolTip = "0";
                                series5.Points[s0].ToolTip = "0";
                                series7.Points[s0].ToolTip = "0";
                                s0++;
                            }

                            if (dt.Rows[i]["SortSorce"].ToString().Equals("0"))
                            {
                                series1.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series3.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                                series5.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                                series7.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                                series1.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                                series3.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                                series5.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                                series7.Points[s0].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                                s0++;
                                monch = dt.Rows[i]["DayTime"].ToString();
                                count = 1;
                                sort = "0";
                            }
                            else
                            {
                                series2.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series4.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString());
                                series6.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString());
                                series8.Points.AddXY(dt.Rows[i]["DayTime"].ToString().Substring(4) + "月", (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString());
                                series2.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                                series4.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseCountTotal"].ToString())).ToString();
                                series6.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseAmountTotal"].ToString()) / 10000).ToString();
                                series8.Points[s1].ToolTip = (double.Parse(dt.Rows[i]["WareHouseCountTotal"].ToString())).ToString();
                                count++;
                                count = 2;
                                sort = "1";

                            }

                        }

                    }
                }
                #endregion

                #region 数据绑定到图
                series1.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series1);
                ChartPurchasing.Series["基本药品采购金额 单位：万元"].ChartArea = "ChartTypePurchasing";

                series2.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series2);
                ChartPurchasing.Series["非基本药品采购金额 单位：万元"].ChartArea = "ChartTypePurchasing";

                series3.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series3);
                ChartInStorage.Series["基本药品采购数量 单位：个"].ChartArea = "ChartTypeStorage";

                series4.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series4);
                ChartInStorage.Series["非基本药品采购数量 单位：个"].ChartArea = "ChartTypeStorage";

                series5.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series5);
                ChartPurchasing.Series["基本药品入库金额 单位：万元"].ChartArea = "ChartTypePurchasing";


                series6.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series6);
                ChartPurchasing.Series["非基本药品入库金额 单位：万元"].ChartArea = "ChartTypePurchasing";

                series7.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series7);
                ChartInStorage.Series["基本药品入库数量 单位：个"].ChartArea = "ChartTypeStorage";

                series8.IsValueShownAsLabel = false;
                ChartInStorage.Series.Add(series8);
                ChartInStorage.Series["非基本药品入库数量 单位：个"].ChartArea = "ChartTypeStorage";
                #endregion

                #region 两张图片属性
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].Area3DStyle.Enable3D = false;

                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisX.IsMarginVisible = true;
                ChartInStorage.ChartAreas["ChartTypeStorage"].Area3DStyle.Enable3D = false;


                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.MajorGrid.Interval = 1;
                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisX.MajorGrid.Interval = 1;
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisY.LineWidth = 1;
                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisY.LineWidth = 1;

                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisY.Title = "金额（万元）";
                ChartPurchasing.ChartAreas["ChartTypePurchasing"].AxisX.Title = "月份";

                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisY.Title = "数量";
                ChartInStorage.ChartAreas["ChartTypeStorage"].AxisX.Title = "月份";
                #endregion

                #region 分组
                ChartPurchasing.Series["基本药品采购金额 单位：万元"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["非基本药品采购金额 单位：万元"]["StackedGroupName"] = "Group1";
                ChartInStorage.Series["基本药品采购数量 单位：个"]["StackedGroupName"] = "Group1";
                ChartInStorage.Series["非基本药品采购数量 单位：个"]["StackedGroupName"] = "Group1";

                ChartPurchasing.Series["基本药品入库金额 单位：万元"]["StackedGroupName"] = "Group2";
                ChartPurchasing.Series["非基本药品入库金额 单位：万元"]["StackedGroupName"] = "Group2";
                ChartInStorage.Series["基本药品入库数量 单位：个"]["StackedGroupName"] = "Group2";
                ChartInStorage.Series["非基本药品入库数量 单位：个"]["StackedGroupName"] = "Group2";
                ChartInStorage.ResetAutoValues();
                ChartPurchasing.ResetAutoValues();
                #endregion
                lblnull.Visible = false;
            }
            else
            {
                this.ChartInStorage.Visible = false;
                this.ChartPurchasing.Visible = false;
                lblnull.Visible = true;
            }

        }

        /// <summary>
        /// 绑定超链接列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwsort_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                try
                {
                    query.Mid = e.CommandArgument.ToString();
                }
                catch { }
                Session[sessionid] = query;
                string url = "";
                if (string.IsNullOrEmpty(query.SortName))
                {
                    url = "SortofMonth.aspx";
                }
                else   //按医院查询
                {
                    url = "SortCurrentGoodsList.aspx";
                }
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }

    }
}
